package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.*;
import com.example.xpvideo.entity.req.PhpVideoReq;
import com.example.xpvideo.entity.req.SupplierMerchantReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.PhpMerchantsRes;
import com.example.xpvideo.entity.res.PhpVideoRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.PhpVideoMapper;
import com.example.xpvideo.mapper.VideoMapper;
import com.example.xpvideo.service.PhpVideoService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/9/14 15:25
 * @description:
 */
@Service
public class PhpVideoServiceImpl implements PhpVideoService {
    @Autowired
    private PhpVideoMapper phpVideoMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private TimeService timeService;

    @Override
    public ResponseResult<PageResult<List<PhpVideoRes>>> list(PhpVideoReq phpVideoReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String title = phpVideoReq.getTitle();
        String phone = phpVideoReq.getPhone();
        Integer userType = phpVideoReq.getUserType();
        String nickname = phpVideoReq.getNickname();

        PageHelper.startPage(page, size);
        List<PhpVideoRes> list = phpVideoMapper.videoList(Wrappers.<Video>query()
                .like(!ObjectUtils.isEmpty(title), "title", title)
                .eq(!ObjectUtils.isEmpty(phone), "v.phone", phone)
                .eq(!ObjectUtils.isEmpty(userType), "user_type", userType)
                .like(!ObjectUtils.isEmpty(nickname),"nickname", nickname));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<PhpVideoRes>> listPageResult = new PageResult<>(count,pages,
                pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> delete(UpdateReq updateReq) throws ErrorException{
        Video video = new Video();
        video.setId(updateReq.getId());
        video.setIsDel(updateReq.getStage());
        video.setUpdateTime(timeService.getTime());
        int res = videoMapper.updateVideo(video);

        if(res > 0){
            return ResponseResult.responseResult(0,"短视频删除成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20003);
    }
}
